package com.gateway.common.exception;

import com.gateway.common.enums.ErrorCode;

import java.io.Serial;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/5 20:21
 **/
public abstract class BaseException extends RuntimeException {
    @Serial
    private static final long serialVersionUID = -4672693392614976093L;

    private final ErrorCode errorCode;

    public BaseException(ErrorCode errorCode) {
        super(errorCode.getMessage());
        this.errorCode = errorCode;
    }

    public BaseException(ErrorCode errorCode, String... message) {
        super(appendErrorMsg(errorCode, message));
        this.errorCode = errorCode;
    }

    public Integer getErrorCode() {
        return errorCode.getCode();
    }

    protected static String appendErrorMsg(ErrorCode errorCode, String... args) {
        String errorMsg = String.format(errorCode.getMessage(), args);
        if (errorMsg != null && errorMsg.equals(errorCode.getMessage())) {
            StringBuffer sb = new StringBuffer();
            sb.append(errorCode.getMessage()).append("，详情：");
            if (args != null && args.length > 0) {
                String[] var4 = args;
                int var5 = args.length;

                for(int var6 = 0; var6 < var5; ++var6) {
                    String arg = var4[var6];
                    sb.append(arg).append(" ");
                }
            }

            errorMsg = sb.toString();
        }

        return errorMsg;
    }
}
